import lib2to3.pgen2.token
from typing import List


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        result = None
        for i in range(0, len(lists)):
            result = self.mergeTwoLists(result,lists[i])
        return result

    @staticmethod
    def mergeTwoLists(l1: ListNode, l2: ListNode):
        """
        合并两个有序链表的函数
        :param l1:
        :param l2:
        :return:
        """
        p = result = ListNode(0)
        while l1 and l2:
            if l1.val < l2.val:
                p.next = l1
                l1 = l1.next
            else:
                p.next = l2
                l2 = l2.next
            p = p.next
        p.next = l1 if l1 else l2
        return result.next